package com.example.design.bilibili.prototype.deep.serialize;

import java.io.*;

public class DeepPrototypeType implements Serializable,Cloneable {

    public String name;//String属性

    public DeepCloneableTarget deepCloneableTarget;//引用类型

    public DeepPrototypeType() {
        super();
    }

    //深拷贝，-方法1：使用clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {

        Object deep = null;
        //这里完成对基本数据类型和String的克隆
        deep = super.clone();
        //对应用类型的属性，进行单独处理
        DeepPrototypeType deepPrototypeType = (DeepPrototypeType) deep;
        deepPrototypeType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();

        return deepPrototypeType;
    }

    //深拷贝-方法2：通过对象的序列化实现（推荐）

    public Object deepClone(){
        //创建对象流
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;

        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);//当前这个对象以对象流的方式输出

            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DeepPrototypeType copyObj = (DeepPrototypeType) ois.readObject();
            return copyObj;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return null;
    }
}
